package com.qf.qfleadnewscommons.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SmsOperator {

    private com.aliyun.dysmsapi20170525.Client client;

    private SMSProperteis properteis;

    public SmsOperator(Client client,SMSProperteis properteis) {
        this.client = client;
        this.properteis = properteis;
    }

    public void sendSms(String phone,String code){
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(properteis.getSignName())
                .setTemplateCode(properteis.getTemplateCode())
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            log.info("----------------开始发送短信" + properteis);
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("----------------发送短信完成");
        } catch (TeaException error) {
            error.printStackTrace();
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            _error.printStackTrace();
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}
